// import { MutationTree, ActionTree } from 'vuex'
import { signIn, info } from "@/apis/user";
import { setToken } from "@/utils/auth";

export default {
    namespaced: true,
    state: {
        token: "",
        userInfo: {}
    },
    mutations: {
        M_setToken(state: any, payload: any): void {
            state.token = payload;
        },
        M_setInfo(state: any, payload: any) {
            state.userInfo = payload;
        },
        M_loginOut(state: any) {
            state.token = "";
            localStorage.clear();
            setToken("");
        }
    },
    actions: {
        A_login({ commit }: any, payload: any) {
            return new Promise<void>(async (resole, reject) => {
                let { status, data } = await signIn(payload);
                if (status) {
                    commit("M_setToken", data);
                    setToken(data);
                    resole(data);
                } else {
                    reject(data);
                }
            });
        },
        async A_getInfo({ commit }: any) {
            let { status, data } = await info();
            if (status) {
                commit("M_setInfo", data);
                localStorage.setItem("userInfo", JSON.stringify(data));
            }
        },

        A_loginOut({ commit }: any) {
            return new Promise((resolve, reject) => {
                commit("M_loginOut");
                resolve("");
            });
        }
    },
    modules: {}
};
